package com.bigdata

import scala.io.Source

object Demo23Case {

  def main(args: Array[String]): Unit = {
    /**
     * 2、统计学生的总分
     */
    //1、读取分数表
    val scoresList: List[String] = Source.fromFile("data/score.txt").getLines().toList

    //2、过滤脏数据
    val filterScore: List[String] = scoresList.filter(line => {
      val arr: Array[String] = line.split(",")
      arr.length == 3 && arr.last.toInt != 0
    })

    val mapScore: List[Array[String]] = filterScore.map(line => {
      line.split(",")
    })

    //3、取出学号和分数
    val pariScore: List[(String, Int)] = mapScore.map {
      case Array(id: String, source: String, score: String) =>
        // println(s"$id---$source---$score")
        (id, score.toInt)
    }

    //4、按照学号分组
    val groupScore: Map[String, List[(String, Int)]] = pariScore.groupBy(kv => kv._1)

    //5、统计学生的总分
    val result: Map[String, Int] = groupScore.map {
      case (id: String, scores: List[(String, Int)]) =>
        val scos: List[Int] = scores.map(kv => kv._2)
        (id, scos.sum)
    }

    result.foreach(println)
  }

}
